package com.adams.common;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.adams.access.db.bean.User;
import com.adams.common.Certificate;
import com.adams.common.Constants;

/**
 * 
 * 
 * File Name : AuthorityFilter.java
 * 
 * @Description : 权限拦截器
 * @author 902990封文进
 */
public class AuthorityFilter implements Filter
{
	private HttpServletRequest request;

	private HttpServletResponse response;

	private String encoding;

	private String exclude;

	private String noCheckPermission;

	/**
	 * 执行检查权限
	 */
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException
	{
		this.request = (HttpServletRequest) request;
		this.response = (HttpServletResponse) response;
		// 设置编码
		this.request.setCharacterEncoding(encoding);
		this.response.setCharacterEncoding(encoding);

		String requestURI = this.request.getRequestURI();

		// 检查是否需要验证
		boolean isValidate = true;
		if (StringUtils.isNotEmpty(exclude))
		{
			String[] arr = exclude.split(",");
			for (int i = 0; i < arr.length; i++)
			{
				if (requestURI.indexOf(arr[i]) >= 0)
				{
					isValidate = false;
					break;
				}
			}
		}

		if (isValidate)
		{
			User user = Certificate.getUser(this.request);
			// 超时
			if (user == null)
			{
				sendError(Constants.OVER_TIME);
				return;
			}
			// 根据配置检查是否要权限控制
			if (StringUtils.isNotEmpty(noCheckPermission))
			{
				String[] arr = noCheckPermission.split(",");
				for (int i = 0; i < arr.length; i++)
				{
					if (requestURI.indexOf(arr[i]) >= 0)
					{
						isValidate = false;
						break;
					}
				}
			}
			if (isValidate)
			{
				// 验证
				if (!Certificate.validate(this.request, user, requestURI))
				{
					sendError(Constants.NO_RIGHT_ACCESS);
					return;
				}
			}
		}
		try
		{
			chain.doFilter(request, response);
		} catch (Exception e)
		{
			sendError("对不起，系统错误！");
		}

	}

	/**
	 * 
	 * Description : 发送错误信息
	 * 
	 * @param errorInfo
	 * @throws IOException
	 * 
	 */
	private void sendError(String errorInfo) throws IOException
	{
		Map result = new HashMap();
		result.put("failure", true);
		result.put("msg", errorInfo);
		if (Constants.OVER_TIME.equals(errorInfo))
		{
			result.put("callback", "location.href='" + request.getContextPath()
					+ "/login.jsp'");
		}
		this.response.setContentType("text/html;charset=UTF-8");
		this.response.getWriter().write(
				JSONObject.fromObject(result).toString());

	}

	/**
	 * 初始化
	 */
	public void init(FilterConfig filterConfig) throws ServletException
	{
		this.encoding = filterConfig.getInitParameter("encoding");
		this.exclude = filterConfig.getInitParameter("exclude");
		this.noCheckPermission = filterConfig
				.getInitParameter("noCheckPermission");

	}

	/**
	 * 销毁
	 */
	public void destroy()
	{

	}

}
